package com.xoa.service.party;


import com.xoa.dao.party.CommunitySchoolMapper;
import com.xoa.model.party.CommunitySchool;
import com.xoa.util.ExcelUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.log.FileUtils;
import com.xoa.util.page.PageParams;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.Transient;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommunitySchoolService {
    @Autowired
    private CommunitySchoolMapper communitySchoolMapper;

    /**
     * 查询
     * @return
     */
    public ToJson select(HttpServletRequest request, CommunitySchool communitySchool, Integer page,
                         Integer pageSize, boolean useFlag, String export , HttpServletResponse response){
        ToJson json=new ToJson();
        Map<String,Object> map=new HashMap<String,Object>();
        PageParams params = new PageParams();
        try{
//            map.put("sid",communitySchool.getSid());
//            map.put("schoolName",communitySchool.getSchoolName());
//            map.put("page",page);
//            map.put("pageSize",pageSize);
            params.setPage(page);
            params.setPageSize(pageSize);
            map.put("communitySchool",communitySchool);
            map.put("params",params);
            List<CommunitySchool> lxdwxxcjs=communitySchoolMapper.selectSchool(map);
            if(export==null){
                export="0";
            }
            if (export.equals("1")){
                if (lxdwxxcjs !=null && lxdwxxcjs.size()>0) {
                    for (CommunitySchool list:lxdwxxcjs){
                        if (list.getSchoolLx()!=null){
                            if (1==list.getSchoolLx()){
                                list.setType("幼儿园");
                            }else if (2==list.getSchoolLx()){
                                list.setType("小学");
                            }else if (3==list.getSchoolLx()){
                                list.setType("成人小学");
                            }else if (4==list.getSchoolLx()){
                                list.setType("普通初中");
                            }else if (5==list.getSchoolLx()){
                                list.setType("职业初中");
                            }else if (6==list.getSchoolLx()){
                                list.setType("成人初中");
                            }else if (7==list.getSchoolLx()){
                                list.setType("普通高中");
                            }else if (8==list.getSchoolLx()){
                                list.setType("成人高中");
                            }else if (9==list.getSchoolLx()){
                                list.setType("中等职业学校");
                            }else if (10==list.getSchoolLx()){
                                list.setType("工读学校");
                            }else if (11==list.getSchoolLx()){
                                list.setType("普通高等学校");
                            }else if (12==list.getSchoolLx()){
                                list.setType("成人高等学校");
                            }else if (13==list.getSchoolLx()){
                                list.setType("特殊教育");
                            }else if (14==list.getSchoolLx()){
                                list.setType("培养研究生的科研机构");
                            }else if (15==list.getSchoolLx()){
                                list.setType("民办的其他高等教育机构");
                            }else if (16==list.getSchoolLx()){
                                list.setType("中等职业培训机构");
                            }else {
                                list.setType("");
                            }
                        }
                    }
                }
                HSSFWorkbook workbook1 = ExcelUtil.makeExcelHead("校园基本信息采集", 9);
                String[] secondTitles = {"学校名称", "地址","学校类型", "所属部门", "占地面积","总占地面",
                        "车数量","安防摄像头数量","已备案摄像头","值班电话", "校长名字",
                        "校长联系方式","教室数量","班级数量","教师数量","在校学生人数","住宿人数",
                        "少数民族人数","外籍学生人数","分管安全责任人姓名","分管安全责任人联系方式",
                        "安全保卫责任人姓名","安全保卫责任人联系方式", "治安责任人姓名",
                        "治安责任人联系方式","安全保卫人数","备注","消费主管部门","派出所名称","派出所电话"};
                HSSFWorkbook workbook2 = ExcelUtil.makeSecondHead(workbook1, secondTitles);
                String[] beanProperty = {"schoolName", "addresss","type", "schoolSsbm", "zdmj","zjzmj",
                        "tccount","afsxtcount","afsxtcountti","zbtel", "schoolzaName",
                        "schoolzaTel", "jaoshiCount", "classCount","teachCount","studentCount","zhushuCount",
                        "shaoshuCount", "wjxsCount","fgaqbwzrrName", "fgaqbwzrrTel",
                        "aqbwzrrName","aqbwzrrTel","zazrrName","zazrrTel","abrsCount","benz", "xfzgbw","sdpcs",
                        "pcsTel"};
                HSSFWorkbook workbook = null;
                workbook = ExcelUtil.exportExcelData(workbook2, lxdwxxcjs, beanProperty);
                OutputStream out = response.getOutputStream();
                String filename = "校园基本信息采集信息记录表.xls";
                filename = FileUtils.encodeDownloadFilename(filename, request.getHeader("user-agent"));
                response.setContentType("application/vnd.ms-excel;charset=UTF-8");
                response.setHeader("content-disposition", "attachment;filename=" + filename);
                workbook.write(out);
                out.close();
            }
           // int count=communitySchoolMapper.selectSchoolCount(communitySchool.getSchoolName());
            json.setObj(lxdwxxcjs);
            json.setTotleNum(params.getTotal());
            json.setMsg("ok");
            json.setFlag(0);
            return json;
        }catch (Exception e){
            json.setMsg("ok");
            json.setFlag(0);
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 修改
     * @return
     */
    @Transient
    public ToJson update(HttpServletRequest request, CommunitySchool communityQfqz){
        ToJson json=new ToJson();
        try{
            int count=communitySchoolMapper.updateByPrimaryKey(communityQfqz);
            if(count>0){
                json.setFlag(0);
                json.setMsg("ok");
                return json;
            }
            json.setFlag(0);
            json.setMsg("修改失败");
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 删除
     * @return
     */
    @Transient
    public ToJson delete(Integer sid){
        ToJson json=new ToJson();
        try{
            int count=communitySchoolMapper.deleteByPrimaryKey(sid);
            if(count>0){
                json.setFlag(0);
                json.setMsg("ok");
                return json;
            }
            json.setFlag(0);
            json.setMsg("删除失败");
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }


    /**
     * 添加
     * @return
     */
    @Transient
    public ToJson add(HttpServletRequest request, CommunitySchool communitySchool){
        ToJson json=new ToJson();
        try{
            int count=communitySchoolMapper.insert(communitySchool);
            if(count>0){
                json.setFlag(0);
                json.setMsg("ok");
                return json;
            }
            json.setFlag(0);
            json.setMsg("添加失败");
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }
}
